//
//  CustomTabBar.swift
//  weibo1
//
//  Created by changhongliang on 2017/7/21.
//  Copyright © 2017年 richinfo. All rights reserved.
//

import UIKit

class CustomTabBar: UITabBar {
    /*自定义TabBar的步骤：
      1.继承系统的TabBar
      2.重写layoutSubviews，在这个里面添加子控件和调整子控件的位置
      3.利用kv重新设置TabBar
    */
    
    override func layoutSubviews() {
        super.layoutSubviews()
        addSubview(centerBtn)
    
        var tabBarArrray  = [UIView]()
        for view in self.subviews {
            if "\(view.classForCoder)" == "UITabBarButton"{
                tabBarArrray.append(view)
            }
        }
        
      let barWidth  = self.bounds.size.width
      let barHeight = self.bounds.size.height
      let centerBtnWidth = self.centerBtn.frame.width
      self.centerBtn.center = CGPoint(x: barWidth/2.0, y: barHeight/2.0)
     //重新布局其他控件
      let barItemWidth = (barWidth - centerBtnWidth)/CGFloat(tabBarArrray.count)
     //逐个布局tabBarItem，修改UITabBarButton的frame 
        for barItem in tabBarArrray.enumerated(){
            var frame  = barItem.element.frame
            if barItem.offset >= tabBarArrray.count/2{
                frame.origin.x = CGFloat(barItem.offset)*barItemWidth + centerBtnWidth
            }else{
                frame.origin.x = CGFloat(barItem.offset)*barItemWidth
            }
            //重新设置frame宽度
           barItem.element.frame = frame
        }
        self.bringSubview(toFront: self.centerBtn)
    }
    
    
    
    lazy var centerBtn:UIButton = {
        let btn = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
        btn.frame.size = CGSize(width: 60, height: 38)
        return btn
    }()
    
   }
